/**
 * Created by TangZK on 2015/5/15.
 */
var mysql = require('mysql');

var db = (function () {
	var sql_host = 'localhost';
	var sql_user = 'root';
	var sql_pass = '123456';
	var sql_database = 'frilly';
	//var sql_host = 'sql.frilly.com';
	//var sql_user = 'frilly';
	//var sql_pass = 'Rq2tu7f7MX5cYcMG';
	//var sql_database = 'frilly';
	var sql_conn = {host: sql_host, user: sql_user, password: sql_pass, database: sql_database};
	var link = null;
	return {
		link: function () {
			if (link == null) {
				link = mysql.createConnection(sql_conn);
			}
		},
		add: function (sql, data, callback) {
			var qry = link.query(sql, data, callback);
			console.log(qry.sql);
		},
		select: function (sql, whereArr, callback) {
			var qry = link.query(sql, whereArr, callback);
			console.log(qry.sql);
		},
		update: function (sql, whereArr, callback) {
			var qry = link.query(sql, whereArr, callback);
			console.log(qry.sql);
		},
		unlink: function () {
			if (link) {
				link.end(function (err) {
					link = null;
					if (err) {
						console.log(err);
					}
				});
			}
		}
	}
})();

function ChatModel() {
	this.name = 'chat-model';
}

ChatModel.prototype.record = function (data, fn) {
	var sql = 'INSERT INTO ecs_chat SET ?';
	db.link();
	db.add(sql, data, function (err, result) {
		db.unlink();
		if (err) throw err;
		fn && fn.call(null, result);
	});
};

ChatModel.prototype.get_chat_list = function (whereArr, fn) {
	var cSql = 'SELECT COUNT(*) cnt FROM ecs_chat WHERE uid=? AND to_uid=? AND read_time=0 LIMIT 1';
	db.link();
	db.select(cSql, whereArr, function(err, result) {
		if(result && result[0] && result[0]['cnt'] > 0) {
			var sql = 'SELECT * FROM ecs_chat WHERE uid=? AND to_uid=? AND read_time=0 ORDER BY send_time DESC';
			db.select(sql, whereArr, function (err, result) {
				db.unlink();
				fn && fn.call(this, result, 0);
			});
		} else {
			whereArr.push(whereArr[1], whereArr[0]);
			var sql = 'SELECT * FROM ecs_chat WHERE (uid=? AND to_uid=?) OR (uid=? AND to_uid=?) ORDER BY send_time DESC LIMIT 3';
			db.select(sql, whereArr, function (err, result) {
				db.unlink();
				fn && fn.call(this, result, 1);
			});
		}
	});
};

ChatModel.prototype.change_msg_state = function (dataArr, fn) {
	var sql = 'UPDATE ecs_chat SET read_time=? WHERE uid=? AND to_uid=? AND read_time=0';
	db.link();
	db.update(sql, dataArr, function (err, result) {
		db.unlink();
		fn && fn.call(this, result);
	});
};

ChatModel.prototype.get_history_msg_list = function (whereObj, page, fn) {
	page = page > 0 ? page : 1;
	var size = 10;
	var fixN = 3;
	var limitStr = ' LIMIT '+(((page - 1) * size) + fixN) + ',' + size;
	var whereArr;
	if(whereObj.send_time) {
		whereArr = [whereObj.send_time, whereObj.uid, whereObj.chat_uid, whereObj.chat_uid, whereObj.uid];
	}
	else {
		whereArr = [whereObj.uid, whereObj.chat_uid, whereObj.chat_uid, whereObj.uid];
	}
	var sql = 'SELECT * FROM ecs_chat WHERE';
	if(whereArr.length > 4) {
		sql += ' send_time<? AND';
	}
	sql += ' (uid=? AND to_uid=?) OR (uid=? AND to_uid=?)';
	sql += ' ORDER BY send_time DESC';
	sql += limitStr;
	db.link();
	db.select(sql, whereArr, function (err, result) {
		db.unlink();
		fn && fn.call(this, result);
	});
};

exports.chatModel = new ChatModel();
